//
//  CXErrorViewController.m
//  Travel
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXErrorViewController.h"
#import "CXNetHandle.h"

@interface CXErrorViewController ()

@property (nonatomic, strong) UIImageView *img_wait;
@property (nonatomic, strong) NSMutableArray *arr_wait;

@end

@implementation CXErrorViewController

- (void)viewWillAppear:(BOOL)animated
{
//    UIButton *btn_back = [UIButton buttonWithType:UIButtonTypeSystem];
//    //[btn_back setTintColor:[UIColor whiteColor]];
//    [btn_back setImage:[UIImage imageNamed:@"leftBack" ] forState:UIControlStateNormal];
//    btn_back.frame = CGRectMake(10, 30, 30, 30);
//    [self.view addSubview:btn_back];
//    [btn_back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}

- (void)back
{
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.93 alpha:1];
    self.img_wait = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    self.img_wait.center = self.view.center;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"0" ofType:@"png"];
    UIImage *img = [UIImage imageWithContentsOfFile:path];
    self.img_wait.image = img;

    [self.view addSubview:self.img_wait];
    
    UILabel *label_wait = [[UILabel alloc] initWithFrame:CGRectMake(self.img_wait.frame.origin.x, self.img_wait.frame.origin.y + self.img_wait.frame.size.height + 10, 200, 50)];
    label_wait.center = CGPointMake(self.view.frame.size.width / 2, self.img_wait.frame.origin.y + self.img_wait.frame.size.height + 20);
    label_wait.textAlignment = NSTextAlignmentCenter;
    label_wait.text = @"点击重新加载";
    [self.view addSubview:label_wait];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    [CXBaseViewController networkReachabilityStatus:^(id result) {
//        NSLog(@"%@", result);
//                if ([result isEqualToNumber:@0]) {
//                    UIAlertController *altController = [UIAlertController alertControllerWithTitle:@"亲 " message:[NSString stringWithFormat:@"%@", @"您的网络状态不佳哦"] preferredStyle:(UIAlertControllerStyleAlert)];
//                    [self presentViewController:altController animated:YES completion:^{
//        
//                    }];
//                    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
//                        NSLog(@"你还能咋地?");
//                        [altController dismissViewControllerAnimated:YES completion:^{
//        
//                        }];
//                    }];
//                    [altController addAction:cancle];
//                    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//                        NSLog(@"想咋地就咋地");
//                        
//                    }];
//                    [altController addAction:sure];
//                }
//    }];
    
    
    
    NSString *str_url = self.str_url;
    [CXNetHandle getUrl:str_url cookie:nil result:^(id result) {
        
        if (result != nil) {
            [self dismissViewControllerAnimated:YES completion:^{
                self.Error(result);
            }];
        }else{
            [self getImgArr];
            self.img_wait.animationImages = self.arr_wait;
            self.img_wait.animationDuration = 0.3;
            self.img_wait.animationRepeatCount = 20;
            [self.img_wait startAnimating];
        }
    }];
    
}

- (void)getImgArr
{
    self.arr_wait = [NSMutableArray array];
    for (NSInteger i = 0; i < 4; i++) {
        //NSLog(@"%ld", i);
        NSString *name = [NSString stringWithFormat:@"%ld", i];
        //[NSBundle mainBundle]代表工程文件目录  保存工程中的信息
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
        UIImage *img = [UIImage imageWithContentsOfFile:path];
        //可变数组添加之前  必须先初始化
        [self.arr_wait addObject:img];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
